package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.SjProjectDesignFun;
import com.ruoyi.gcgl.mapper.SjProjectDesignFunMapper;
import com.ruoyi.gcgl.service.ISjProjectDesignFunService;

/**
 * 项目设计工作联系函Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class SjProjectDesignFunServiceImpl implements ISjProjectDesignFunService 
{
    @Autowired
    private SjProjectDesignFunMapper sjProjectDesignFunMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询项目设计工作联系函
     * 
     * @param id 项目设计工作联系函主键
     * @return 项目设计工作联系函
     */
    @Override
    public SjProjectDesignFun selectSjProjectDesignFunById(Long id)
    {
        return sjProjectDesignFunMapper.selectSjProjectDesignFunById(id);
    }

    /**
     * 查询项目设计工作联系函列表
     * 
     * @param sjProjectDesignFun 项目设计工作联系函
     * @return 项目设计工作联系函
     */
    @Override
    public List<SjProjectDesignFun> selectSjProjectDesignFunList(SjProjectDesignFun sjProjectDesignFun)
    {
        return sjProjectDesignFunMapper.selectSjProjectDesignFunList(sjProjectDesignFun);
    }

    /**
     * 新增项目设计工作联系函
     * 
     * @param sjProjectDesignFun 项目设计工作联系函
     * @return 结果
     */
    @Override
    public int insertSjProjectDesignFun(SjProjectDesignFun sjProjectDesignFun)
    {
    	int rows = sjProjectDesignFunMapper.insertSjProjectDesignFun(sjProjectDesignFun);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(sjProjectDesignFun.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", sjProjectDesignFun.getOwner());
        variables.put("level1", sjProjectDesignFun.getFzr());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjhanjian", String.valueOf(sjProjectDesignFun.getId()), variables);
    	
        SjProjectDesignFun obj = new SjProjectDesignFun();
    	obj.setId(sjProjectDesignFun.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	sjProjectDesignFunMapper.updateSjProjectDesignFun(obj);
    	
        return rows;
    }

    /**
     * 修改项目设计工作联系函
     * 
     * @param sjProjectDesignFun 项目设计工作联系函
     * @return 结果
     */
    @Override
    public int updateSjProjectDesignFun(SjProjectDesignFun sjProjectDesignFun)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(sjProjectDesignFun.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", sjProjectDesignFun.getOwner());
//        variables.put("level1", sjProjectDesignFun.getFzr());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjhanjian", String.valueOf(sjProjectDesignFun.getId()), variables);
//        sjProjectDesignFun.setProcessInstanceId(pi.getProcessInstanceId());
    	
        return sjProjectDesignFunMapper.updateSjProjectDesignFun(sjProjectDesignFun);
    }

    /**
     * 批量删除项目设计工作联系函
     * 
     * @param ids 需要删除的项目设计工作联系函主键
     * @return 结果
     */
    @Override
    public int deleteSjProjectDesignFunByIds(String ids)
    {
        return sjProjectDesignFunMapper.deleteSjProjectDesignFunByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目设计工作联系函信息
     * 
     * @param id 项目设计工作联系函主键
     * @return 结果
     */
    @Override
    public int deleteSjProjectDesignFunById(Long id)
    {
        return sjProjectDesignFunMapper.deleteSjProjectDesignFunById(id);
    }

	@Override
	public int updateSjProjectDesignFun4check(
			SjProjectDesignFun sjProjectDesignFun) {
		return sjProjectDesignFunMapper.updateSjProjectDesignFun(sjProjectDesignFun);
	}

	@Override
	public int insertSjProjectDesignFun4check(
			SjProjectDesignFun sjProjectDesignFun) {
		return sjProjectDesignFunMapper.insertSjProjectDesignFun(sjProjectDesignFun);
	}

	@Override
	public List<Map> selectFunAll(SjProjectDesignFun sjProjectDesignFun) {
		return sjProjectDesignFunMapper.selectFunAll(sjProjectDesignFun);
	}
}
